package com.qing.cheng.controller;


import com.qing.cheng.dto.ResponseMessage;
import com.qing.cheng.dto.SuccessRes;
import com.qing.cheng.entity.QcOrder;
import com.qing.cheng.service.OrderService;
import com.qing.cheng.util.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 订单管理
 */
@RequestMapping("/order")
@RestController
public class OrderController {
    public final OrderService orderService;

    private static final Logger log = LoggerFactory.getLogger(OrderController.class);

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }


    @RequestMapping("/newOrder")
    public ResponseMessage newOrder(@RequestBody QcOrder qcOrder, HttpServletRequest request) {
        try {
            orderService.generateNewOrder(qcOrder,request);
            return SuccessRes.success("SUCCESS");
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return null;
        }
    }


    /**
     *
     * @param status 订单状态(0待支付 1已支付 10已取消)
     * @return
     */
    @RequestMapping("/listOrder/{status}")
    public ResponseMessage listOrder(@PathVariable("status")String status, HttpServletRequest request){
        try {
            Integer userId = SessionUtil.getUserId(request);
            return SuccessRes.success(orderService.listOrder(status,request));
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return null;
        }
    }
}
